package com.apes.fn.scm.rebatePolicy.repository;

import com.apes.fn.scm.rebatePolicy.model.RebatePolicy;
import com.apes.fn.scm.rebatePolicy.model.RebatePolicyExecute;
import com.apes.framework.jpa.repository.CustomRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface RebatePolicyExecuteRepository extends CustomRepository<RebatePolicyExecute, String> {

    @Query(value = "SELECT A.* " +
            "  FROM FN_REBATE_POLICY_EXECUTE A " +
            " WHERE ((A.REBATE_DIMENSION = '002468' AND A.SUPPLIER_ID = :supplierId) OR " +
            "       (A.REBATE_DIMENSION = '002467' AND A.CLASSIFY_ID = :classifyId) OR " +
            "       (A.REBATE_DIMENSION = '002469' AND A.CLASSIFY_ID = :classifyId AND A.BRAND_ID = :brandId) OR " +
            "       (A.REBATE_DIMENSION = '002470' AND A.PRODUCT_ID = :productId)) AND A.SUPPLIER_ID = :supplierId AND " +
            "        A.START_DATE <= to_date(:createDate,'yyyy-MM-dd') AND A.END_DATE >= to_date(:createDate,'yyyy-MM-dd') " +
            "       AND A.REBATE_POLICY_ID = :id " +
            "       ORDER BY A.PRIORITY DESC,A.ID DESC",nativeQuery = true)
    List<RebatePolicyExecute> findExecute(@Param("createDate") String createDate, @Param("supplierId") String supplierId, @Param("classifyId") String classifyId, @Param("brandId") String brandId, @Param("productId") String productId, @Param("id") String id);

    List<RebatePolicyExecute> findAllByRebatePolicy(RebatePolicy rebatePolicy);
}
